//
//  CheckOrderView.m
//  mendianjieke
//
//  Created by 响当当 on 9/4/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "CheckOrderView.h"
#import "CheckOrderCell.h"

@interface CheckOrderView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *orderArr;

@property (nonatomic, strong)NSString *totalCount;
@end

@implementation CheckOrderView

- (NSMutableArray *)orderArr
{
    if (!_orderArr)
    {
        _orderArr = [[NSMutableArray alloc]init];
    }
    return _orderArr;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = LCHexColor(0xF2F2F2);
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - delegate & datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.orderArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CheckOrderModel *model = self.orderArr[indexPath.row];
    return model.cellHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 85*kUIScaleBasePlus;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 50*kUIScaleBasePlus;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,86*kUIScaleBasePlus)];
    headView.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [Utilties creatLabelWithFrame:CGRectMake(15*kUIScaleBasePlus,15*kUIScaleBasePlus,120*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                           textAligment:NSTextAlignmentLeft
                                               textClor:LCHexColor(0x282828)
                                               textFont:15*kUIScaleBasePlus];
    titleLabel.text = @"消费清单";
    [headView addSubview:titleLabel];
    
    UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(titleLabel.frame)+15*kUIScaleBasePlus,SCREEN_WIDTH,35*kUIScaleBasePlus)];
    sectionView.backgroundColor = LCHexColor(0xFAFAFA);
    [headView addSubview:sectionView];
    
    UILabel *warseLabel = [Utilties creatLabelWithFrame:CGRectMake(15*kUIScaleBasePlus,15*kUIScaleBasePlus,200*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                           textAligment:NSTextAlignmentLeft
                                               textClor:LCHexColor(0x989898)
                                               textFont:14*kUIScaleBasePlus];
    warseLabel.text = @"商品名称";
    [sectionView addSubview:warseLabel];
    
    UILabel *warseCountLabel = [Utilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(warseLabel.frame)+20*kUIScaleBasePlus,15*kUIScaleBasePlus,50*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                           textAligment:NSTextAlignmentLeft
                                               textClor:LCHexColor(0x989898)
                                               textFont:14*kUIScaleBasePlus];
    warseCountLabel.text = @"数量";
    [sectionView addSubview:warseCountLabel];
    
    UILabel *PriceLabel = [Utilties creatLabelWithFrame:CGRectMake(SCREEN_WIDTH-15*kUIScaleBasePlus-50*kUIScaleBasePlus,15*kUIScaleBasePlus,50*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                                textAligment:NSTextAlignmentRight
                                                    textClor:LCHexColor(0x989898)
                                                    textFont:14*kUIScaleBasePlus];
    PriceLabel.text = @"价格";
    [sectionView addSubview:PriceLabel];
    
    
    return headView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,50*kUIScaleBasePlus)];
    footView.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [Utilties creatLabelWithFrame:CGRectMake(15*kUIScaleBasePlus,15*kUIScaleBasePlus,120*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                           textAligment:NSTextAlignmentLeft
                                               textClor:LCHexColor(0x282828)
                                               textFont:15*kUIScaleBasePlus];
    titleLabel.text = @"合计：";
    [footView addSubview:titleLabel];
    
    
    UILabel *priceLabel = [Utilties creatLabelWithFrame:CGRectMake(SCREEN_WIDTH-15*kUIScaleBasePlus-200*kUIScaleBasePlus,15*kUIScaleBasePlus,200*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                           textAligment:NSTextAlignmentRight
                                               textClor:LCHexColor(0xCD292A)
                                               textFont:15*kUIScaleBasePlus];
    priceLabel.text = [NSString stringWithFormat:@"¥%@",self.totalCount];
    [footView addSubview:priceLabel];
    
    return footView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CheckOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CheckOrderCell"];
    if (!cell)
    {
        cell = [[CheckOrderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CheckOrderCell"];
        cell.selectionStyle = 0;
    }
    
    cell.model = self.orderArr[indexPath.row];
    
    return cell;
}

- (void)getInfo:(TableViewDetailModel *)model
{
    NSString *str = model.detailArr[0];
    
    NSArray *waresArr = [str componentsSeparatedByString:@"],["];
    
    if (waresArr.count > 1)
    {
        for (int i = 0; i<waresArr.count; i++)
        {
            NSString *waresStr = waresArr[i];
            NSArray *arr = [waresStr componentsSeparatedByString:@","];
            
            CheckOrderModel *model = [[CheckOrderModel alloc]init];
            
            if (i == 0)
            {
                NSString *str = arr[0];
                
                model.content = [str substringFromIndex:2];
                model.count = arr[1];
                model.price = arr[2];
            }
            else if(i == arr.count-1)
            {
                NSString *str = arr[2];
                
                model.content = arr[0];
                model.count = arr[1];
                model.price = [str substringToIndex:str.length-2];
            }
            else
            {
                model.content = arr[0];
                model.count = arr[1];
                model.price = arr[2];
            }
            
            model.cellHeight = [CheckOrderCell heightWithModel:model];
            
            [self.orderArr addObject:model];
        }
    }
    else
    {
        NSString *waresStr = waresArr[0];
        NSArray *arr = [waresStr componentsSeparatedByString:@","];
        CheckOrderModel *model = [[CheckOrderModel alloc]init];
        
        NSString *str = arr[0];
        NSString *price = arr[2];

        model.content = [str substringFromIndex:2];
        model.count = arr[1];
        model.price = [price substringToIndex:price.length-2];
        
        model.cellHeight = [CheckOrderCell heightWithModel:model];

        [self.orderArr addObject:model];
        
        

    }
    
    self.totalCount = model.detailArr[1];
    
    [self.tableView reloadData];
}

@end
